package com.obggtc.picture.service.impl;

import com.obggtc.picture.config.AiConfig;
import com.obggtc.picture.dto.ai.AiChatRequest;
import com.obggtc.picture.dto.ai.AiChatResponse;
import com.obggtc.picture.enums.AiProviderEnum;
import com.obggtc.picture.service.AiService;
import com.obggtc.picture.service.ai.AiProvider;
import com.obggtc.picture.service.ai.AiProviderFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * AI服务实现类
 * 
 * @author system
 * @date 2025/09/10
 */
@Slf4j
@Service
public class AiServiceImpl implements AiService {
    
    @Autowired
    private AiConfig aiConfig;
    
    @Autowired
    private AiProviderFactory aiProviderFactory;
    
    @Override
    public AiChatResponse chat(AiChatRequest request) {
        return chat(request, aiConfig.getDefaultProvider());
    }
    
    @Override
    public AiChatResponse chat(AiChatRequest request, String provider) {
        try {
            log.info("收到AI聊天请求，消息：{}，使用提供商：{}", request.getMessage(), provider);
            
            // 参数校验
            if (request == null || !StringUtils.hasText(request.getMessage())) {
                return AiChatResponse.error("消息内容不能为空");
            }
            
            if (!aiConfig.isEnabled()) {
                return AiChatResponse.error("AI服务已禁用");
            }
            
            // 获取指定的AI提供商
            AiProvider aiProvider;
            try {
                aiProvider = aiProviderFactory.getProvider(provider);
            } catch (IllegalArgumentException e) {
                log.error("不支持的AI提供商：{}", provider);
                return AiChatResponse.error("不支持的AI提供商：" + provider);
            }
            
            // 检查服务配置
            if (!aiProvider.isServiceAvailable()) {
                return AiChatResponse.error(provider + " AI服务配置不完整，请检查配置");
            }
            
            // 调用AI服务
            AiChatResponse response = aiProvider.chat(request);
            log.info("AI响应完成，提供商：{}，会话ID：{}", provider, request.getSessionId());
            return response;
            
        } catch (Exception e) {
            log.error("AI聊天服务异常", e);
            return AiChatResponse.error("AI服务异常：" + e.getMessage());
        }
    }
    
    @Override
    public boolean isServiceAvailable() {
        return isServiceAvailable(aiConfig.getDefaultProvider());
    }
    
    @Override
    public boolean isServiceAvailable(String provider) {
        try {
            if (!aiConfig.isEnabled()) {
                return false;
            }
            
            AiProvider aiProvider = aiProviderFactory.getProvider(provider);
            return aiProvider.isServiceAvailable();
        } catch (Exception e) {
            log.error("检查AI服务可用性异常，提供商：{}", provider, e);
            return false;
        }
    }
    
    @Override
    public Map<String, Boolean> getAllProvidersStatus() {
        Map<String, Boolean> statusMap = new HashMap<>();
        
        for (AiProviderEnum providerEnum : AiProviderEnum.values()) {
            try {
                boolean available = isServiceAvailable(providerEnum.getCode());
                statusMap.put(providerEnum.getCode(), available);
            } catch (Exception e) {
                log.error("检查AI提供商状态异常：{}", providerEnum.getCode(), e);
                statusMap.put(providerEnum.getCode(), false);
            }
        }
        
        return statusMap;
    }
    
    @Override
    public boolean testConnection(String provider) {
        try {
            AiProvider aiProvider = aiProviderFactory.getProvider(provider);
            return aiProvider.testConnection();
        } catch (Exception e) {
            log.error("测试AI连接异常，提供商：{}", provider, e);
            return false;
        }
    }
}
